package com.xyh.flowershop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xyh.flowershop.annotations.RefreshToken;
import com.xyh.flowershop.constant.RequestBaseConstant;
import com.xyh.flowershop.service.FileService;
import com.xyh.flowershop.service.GoodsService;
import com.xyh.flowershop.vo.resp.common.ResultRespVo;
import com.xyh.flowershop.vo.req.goods.GoodPageReqVo;
import com.xyh.flowershop.vo.req.goods.GoodsEditReqVo;
import com.xyh.flowershop.vo.resp.goods.GoodsRespVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/goods/")
@Tag(name = "商品信息接口")
public class GoodsController {

    private final GoodsService goodsService;

    private final FileService fileService;

    public GoodsController(GoodsService goodsService, FileService fileService) {
        this.goodsService = goodsService;
        this.fileService = fileService;
    }

    @RefreshToken
    @Operation(description = "商品的编辑或新增")
    @PostMapping("editOrAdd")
    public ResultRespVo editOrAddGoods(@RequestBody GoodsEditReqVo vo){
        Boolean flag;
        if(vo.getId() == null){
            flag = goodsService.addGoods(vo);
        }else{
            flag = goodsService.editGoods(vo);
        }
        return flag ? ResultRespVo.success() : ResultRespVo.error();
    }

    @RefreshToken
    @Operation(description = "删除单个商品")
    @DeleteMapping("/delete/{id}")
    public ResultRespVo deleteGoods(@PathVariable Long id){
        List<Long> ids = new ArrayList<>();
        ids.add(id);
        return goodsService.deleteGoods(ids) ? ResultRespVo.success() : ResultRespVo.error();
    }

    @RefreshToken
    @Operation(description = "批量删除商品")
    @DeleteMapping("delete/batch")
    public ResultRespVo deleteBatchGoods(@RequestBody List<Long> ids){
        return goodsService.deleteGoods(ids) ? ResultRespVo.success() : ResultRespVo.error();
    }

    @Operation(description = "查询商品")
    @PostMapping("app/find/page")
    public ResultRespVo findPageGoods(@RequestBody GoodPageReqVo vo){
        IPage<GoodsRespVo> pages = goodsService.findPageGoods(vo);
        return ResultRespVo.success(pages);
    }

    @Operation(description = "根据编号查找商品")
    @GetMapping("app/find/{id}")
    public ResultRespVo findGoods(@PathVariable Long id){
        return ResultRespVo.success(goodsService.findGoods(id));
    }

    @Operation(description = "文件上传")
    @PostMapping("upload")
    public ResultRespVo uploadFile(@RequestBody MultipartFile file, HttpServletRequest request){
        String path = RequestBaseConstant.BASE_URL+"/img/goods/"+fileService.uploadFile(file, request.getRequestURI());
        return ResultRespVo.success(path);
    }

}
